//
//  SearchNearPointViewModel.m
//  AppointmentForCar
//
//  Created by ZengPengYuan on 2016/11/14.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "SearchNearPointViewModel.h"
#import "GeoCodeSearchManager.h"
#import "SearchKeyWordManager.h"
#import "SearchResultTableViewCell.h"
#import "CommonAddressModel.h"

static NSString * SearchResultTableViewCellID = @"SearchResultTableViewCell";
static NSString * CommonResultTableViewCellID = @"CommonResultTableViewCell";

@interface  SearchNearPointViewModel ()

@property (nonatomic , strong) GeoCodeSearchManager *geoCodeManager;
@property (nonatomic , strong) SearchKeyWordManager *searchKeyWord;
@property (nonatomic , strong) NSArray<BMKPoiInfo *> *dataArray;
@property (nonatomic , copy) void (^clickInfo) (BMKPoiInfo * info);
@property (nonatomic , strong) CommonAddressModel *commonAddress;
@property (nonatomic , weak) UISearchBar *searchBar;

@end

@implementation SearchNearPointViewModel


- (instancetype)init {
    self = [super init];
    if (self) {
        [self getUserCommonAddressWithPassengerId:[UserAccountManager shareUserManager].user.userId complete:^(id error) {
        }];
    }
    return self;
}



- (void)searchCityName:(NSString *)city keyWork:(NSString *)keyWork {
    self.dataArray = nil;
    [self.tableView reloadData];
    __weak __typeof__(self) weakSelf = self;
    [self.searchKeyWord searchCity:city keyWord:keyWork complete:^(NSArray<BMKPoiInfo *> *list, id error) {
        if(!error) {
            __strong __typeof__(weakSelf) strongSelf = weakSelf;
            strongSelf.dataArray = list;
            [strongSelf.tableView reloadData];
        }
    }];
}

- (void)searchClickItem:(void (^) (BMKPoiInfo * info))result {
    _clickInfo = result;
}

- (void)getUserCommonAddressWithPassengerId:(NSString *)Id complete:(void (^) (id error))complete {
    [NetWork postNetWorkWithUrl:@"user-common-address" parame:@{@"id":Id} successBlock:^(NSDictionary *dic) {
        if([dic[@"state"] integerValue] == 0) {
            _commonAddress = [CommonAddressModel mj_objectWithKeyValues:dic[@"data"]];
            [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
        }else {
        }
    } errorBlock:^(id error) {
    }];
}

#pragma mark - tableview Delegate Method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row) {
        // 搜索结果
        SearchResultTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SearchResultTableViewCellID];
        cell.model = self.dataArray[indexPath.row - 1];
        return cell;
    } else {
        // 家／公司
        CommonResultTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CommonResultTableViewCellID];
        cell.model = self.commonAddress;
        return cell;
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1 + self.dataArray.count;
}

//- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 100;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row) {
        return 70;
    } else {
        return 60;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(!indexPath.row) {
        return;
    }
    if(_clickInfo) {
        _clickInfo(_dataArray[indexPath.row - 1]);
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.searchBar resignFirstResponder];
}

#pragma mark - SearchBarDelegate Method
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    _searchBar = searchBar;
    return YES;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    [self searchCityName:_cityName keyWork:searchText];
}

#pragma mark - setter and getter
- (GeoCodeSearchManager *)geoCodeManager {
    if(!_geoCodeManager) {
        _geoCodeManager = [[GeoCodeSearchManager alloc] init];
    }
    return _geoCodeManager;
}

- (SearchKeyWordManager *)searchKeyWord {
    if(!_searchKeyWord) {
        _searchKeyWord = [[SearchKeyWordManager alloc] init];
    }
    return _searchKeyWord;
}

- (void)setTableView:(UITableView *)tableView {
    _tableView = tableView;
    [tableView registerClass:[SearchResultTableViewCell class] forCellReuseIdentifier:SearchResultTableViewCellID];
    [tableView registerClass:[CommonResultTableViewCell class] forCellReuseIdentifier:CommonResultTableViewCellID];
}

@end
